package cn.fy.navigation.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import cn.fy.navigation.R;
import cn.fy.navigation.databinding.ActivityRadioGroupBinding;
import cn.fy.navigation.fragment.Frag1Fragment;
import cn.fy.navigation.fragment.Frag2Fragment;
import cn.fy.navigation.fragment.Frag3Fragment;

public class RadioGroupActivity extends AppCompatActivity {

    private ActivityRadioGroupBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_radio_group);

        binding = ActivityRadioGroupBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("RadioGroup");

        binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_button1:
                        binding.viewPager.setCurrentItem(0);
                        break;
                    case R.id.radio_button2:
                        binding.viewPager.setCurrentItem(1);
                        break;
                    case R.id.radio_button3:
                        binding.viewPager.setCurrentItem(2);
                        break;
                }
            }
        });

        List<Class> fragmentList = new ArrayList<>();
        fragmentList.add(Frag1Fragment.class);
        fragmentList.add(Frag2Fragment.class);
        fragmentList.add(Frag3Fragment.class);

        binding.viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //return listFragment.get(position);
                try {
                    return (Fragment) fragmentList.get(position).newInstance();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });

        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        binding.radioButton1.setChecked(true);
                        break;
                    case 1:
                        binding.radioButton2.setChecked(true);
                        break;
                    case 2:
                        binding.radioButton3.setChecked(true);
                        break;
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}